package server_udp

import (
	"log"
	"net"
)

func send(con *net.UDPConn, addr *net.UDPAddr, msg []byte) {
	_, err := con.WriteToUDP(msg, addr)
	if err != nil {
		log.Println(err)
	}
}

func Receive() {
	log.Println("Start Server UDP Test")
	p := make([]byte, 1024)
	addr := net.UDPAddr{
		Port: 7778,
		IP:   net.ParseIP("127.0.0.1"),
	}

	server, err := net.ListenUDP("udp", &addr)
	if err != nil {
		log.Println(err)
	}
	log.Println("Udp Server UDP listen success")
	defer server.Close()

	for {

		i, addr, err := server.ReadFromUDP(p)
		if err != nil {
			log.Println(err)
		}
		log.Printf("Received message from %v: %v", addr.String(), string(p[:i]))
		go send(server, addr, p[i:])
	}

}
